package conf

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"

	"mutu/misc/typedef"
)

const (
	TimeFormat = "2006-01-02 15:04:05"

	UrlLen   = 1024
	NickLen  = 52
	NameLen  = 128
	TitleLen = 1024
	DescLen  = 4096

	//全局标识
	//1、认证方式
	F_Auth_PW = 0x10001 //口令登录
	F_Auth_QQ = 0x10002 //qq
	F_Auth_WX = 0x10003 //微信
	F_Auth_WB = 0x10004 //微博

	F_Auth_PW_S = "pw" //口令登录
	F_Auth_QQ_S = "qq" //qq
	F_Auth_WX_S = "wx" //微信
	F_Auth_WB_S = "wb" //微博

	//2、用户权限
	F_Role_Normal = 0x20001 //普通用户
	F_Role_Upload = 0x20010 //上传
	F_Role_Admin  = 0x21000 //管理

	//3、功能标识
	F_Action_Resource = 0x30001 //资源
	F_Action_Matter   = 0x30002 //收藏
	F_Action_Binder   = 0x30003 //收藏
	F_Action_Comment  = 0x30004 //评论
	F_Action_Like     = 0x30005 //喜欢
	F_Action_Unlike   = 0x30006 //不喜欢
	F_Action_Access   = 0x30007 //访问量

	//4、资源分类
	F_Resource_PIC  = 0x40001 //灵感图
	F_Resource_PSD  = 0x40002 //psd
	F_Resource_SHOW = 0x40003 //psd

	//4、素材分类
	F_Matter_PIC = 0x50001 //灵感图
	F_Matter_PSD = 0x50002 //psd

	//5、标签分类
	F_Tag_Color    = 0x60001 //颜色标签
	F_Tag_Useage   = 0x60002 //用途
	F_Tag_Festival = 0x60003 //节日
	F_Tag_Emotion  = 0x60004 //情感
	F_Tag_Sport    = 0x60005 //运动

	//6、收藏夹属性
	F_Binder_Pub = 0x70001 //公开收藏
	F_Binder_Pri = 0x70002 //私有收藏

	F_Boss_Income = 0x80001 //收入
	F_Boss_Expend = 0x80002 //支出
)

var (
	DefConfigPath = flag.String("c", "/etc/mutu/mutu/config", "config path")
	Config        typedef.Dict
	WebDomain     = "artui.cn"
	SessionKey    = "SESSIONID"
)

func parseConfig(path string) (map[string]interface{}, error) {

	contents, err := ioutil.ReadFile(path)
	if nil != err {

		return nil, err
	}

	config := typedef.Dict{}

	err = json.Unmarshal(contents, &config)

	if nil != err {

		return nil, err
	}
	return config, nil
}

func init() {
	flag.Parse()
	var err error
	if Config, err = parseConfig(*DefConfigPath); err != nil {
		fmt.Printf("parse config file %s error, %v\n", *DefConfigPath, err)
		os.Exit(1)
	}
	WebDomain = Config["WebDomain"].(string)
	SessionKey = Config["SessionKey"].(string)
}

func GetInt(key string, defalut ...int) int {
	val, ok := Config[key].(float64)
	if !ok {
		if len(defalut) > 0 {
			return defalut[0]
		} else {
			return 0
		}
	}
	return int(val)
}

func GetString(key string, defalut ...string) string {
	val, ok := Config[key].(string)
	if !ok {
		if len(defalut) > 0 {
			return defalut[0]
		} else {
			return ""
		}
	}
	return val
}

func GetBool(key string, defalut ...bool) bool {
	val, ok := Config[key].(bool)
	if !ok {
		if len(defalut) > 0 {
			return defalut[0]
		} else {
			return false
		}
	}
	return val
}

func GetStrings(key string, defalut ...[]string) ([]string, int) {
	val, ok := Config[key].(string)
	if !ok {
		if len(defalut) > 0 {
			return defalut[0], len(defalut[0])
		} else {
			return nil, 0
		}
	}
	val = strings.Trim(val, " ")
	if len(val) == 0 {
		return nil, 0
	}

	strArr := strings.Split(val, ",")
	length := len(strArr)
	arr := make([]string, length)
	i := 0
	for _, v := range strArr {
		nv := strings.Trim(v, " ")
		if len(nv) > 0 {
			arr[i] = nv
			i++
		}
	}
	return arr, i
}

func GetInts(key string, defalut ...[]int) ([]int, int) {
	val, ok := Config[key].(string)
	if !ok {
		if len(defalut) > 0 {
			return defalut[0], len(defalut[0])
		} else {
			return nil, 0
		}
	}
	val = strings.Trim(val, " ")
	if len(val) == 0 {
		return nil, 0
	}

	strArr := strings.Split(val, ",")
	length := len(strArr)
	arr := make([]int, length)
	var err error
	for i, v := range strArr {
		nv := strings.Trim(v, " ")
		arr[i], err = strconv.Atoi(nv)
		if err != nil {
			return nil, 0
		}
	}
	return arr, length
}
